package com.example.springcloud.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.example.springcloud.domain.UserRouteFavorite;
import com.example.springcloud.mapper.UserScenicFavoriteMapper;
import com.example.springcloud.request.UserCancelScenicFavoriteRequest;
import com.example.springcloud.request.UserScenicFavoriteRequest;
import io.swagger.annotations.*;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.User;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.example.springcloud.domain.UserScenicFavorite;
import com.example.springcloud.service.IUserScenicFavoriteService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 用户景点收藏Controller
 * 
 * @author helian
 * @date 2025-03-09
 */
@RestController
@RequestMapping("/jiu-mount-huangshan/userScenicFavorite")
@Api(tags = "用户景点收藏管理")
public class UserScenicFavoriteController extends BaseController
{
    @Autowired
    private IUserScenicFavoriteService userScenicFavoriteService;

    @Autowired
    private UserScenicFavoriteMapper userScenicFavoriteMapper;

    /**
     * app查询用户景点收藏列表
     */
    @GetMapping("/list/app")
    @ApiOperation(value = "查询用户景点收藏列表")
    @ApiImplicitParams({@ApiImplicitParam(name = "userId", value = "用户id", required = true, dataType = "Integer")})
    @ApiResponses(@ApiResponse(code = 200, message = "查询用户景点收藏列表", response = UserScenicFavorite.class , responseContainer = "List"))
    public TableDataInfo list(Integer userId)
    {
        startPage();
        UserScenicFavorite userScenicFavorite = new UserScenicFavorite();
        userScenicFavorite.setUserId(Long.valueOf(userId));
        List<UserScenicFavorite> list = userScenicFavoriteService.selectUserScenicFavoriteList(userScenicFavorite);
        return getDataTable(list);
    }

    /**
     * 查询用户景点收藏列表
     */
    @GetMapping("/list")
    public TableDataInfo list(UserScenicFavorite userScenicFavorite)
    {
        startPage();
        List<UserScenicFavorite> list = userScenicFavoriteService.selectUserScenicFavoriteList(userScenicFavorite);
        return getDataTable(list);
    }

    /**
     * 用户收藏景点
     */
    @PostMapping("/add")
    @ApiOperation(value = "用户收藏景点")
    @ApiImplicitParams({@ApiImplicitParam(name = "userScenicFavoriteRequest", value = "用户收藏景点请求对象", required = true, dataType = "UserScenicFavoriteRequest")})
    @ApiResponses(@ApiResponse(code = 200, message = "用户收藏景点成功", response = UserScenicFavorite.class))
    public AjaxResult add(@RequestBody  UserScenicFavoriteRequest userScenicFavoriteRequest)
    {
        // 用户已经收藏过该景点，不能重复收藏
        if (userScenicFavoriteMapper.selectUserScenicFavoriteByScenicIdAndUserId(userScenicFavoriteRequest.getScenicId(), userScenicFavoriteRequest.getUserId()) != null) {
            return AjaxResult.error("该景点已收藏");
        }
        UserScenicFavorite userScenicFavorite = new UserScenicFavorite();
        userScenicFavorite.setUserId(userScenicFavoriteRequest.getUserId());
        userScenicFavorite.setScenicId(userScenicFavoriteRequest.getScenicId());
        return toAjax(userScenicFavoriteService.insertUserScenicFavorite(userScenicFavorite));
    }

    /**
     * 用户取消收藏景点
     */
    @DeleteMapping("/delete")
    @ApiOperation(value = "用户取消收藏景点")
    @ApiImplicitParams({@ApiImplicitParam(name = "userId", value = "用户id", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "scenicId", value = "景点id", required = true, dataType = "Integer")})
    @ApiResponses(@ApiResponse(code = 200, message = "用户取消收藏景点成功", response = UserScenicFavorite.class))
    public AjaxResult delete(@RequestBody UserCancelScenicFavoriteRequest userCancelScenicFavoriteRequest)
    {
        return toAjax(userScenicFavoriteService.deleteUserScenicFavoriteByUserIdAndScenicId(userCancelScenicFavoriteRequest.getUserId(), userCancelScenicFavoriteRequest.getScenicId()));
    }


    /**
     * 导出用户景点收藏列表
     */
    @PreAuthorize("@ss.hasPermi('jiu-mount-huangshan:userScenicFavorite:export')")
    @Log(title = "用户景点收藏", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, UserScenicFavorite userScenicFavorite)
    {
        List<UserScenicFavorite> list = userScenicFavoriteService.selectUserScenicFavoriteList(userScenicFavorite);
        ExcelUtil<UserScenicFavorite> util = new ExcelUtil<UserScenicFavorite>(UserScenicFavorite.class);
        util.exportExcel(response, list, "用户景点收藏数据");
    }

    /**
     * 获取用户景点收藏详细信息
     */
    @PreAuthorize("@ss.hasPermi('jiu-mount-huangshan:userScenicFavorite:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(userScenicFavoriteService.selectUserScenicFavoriteById(id));
    }

    /**
     * 修改用户景点收藏
     */
    @PreAuthorize("@ss.hasPermi('jiu-mount-huangshan:userScenicFavorite:edit')")
    @Log(title = "用户景点收藏", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody UserScenicFavorite userScenicFavorite)
    {
        return toAjax(userScenicFavoriteService.updateUserScenicFavorite(userScenicFavorite));
    }

    /**
     * 删除用户景点收藏
     */
    @PreAuthorize("@ss.hasPermi('jiu-mount-huangshan:userScenicFavorite:remove')")
    @Log(title = "用户景点收藏", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(userScenicFavoriteService.deleteUserScenicFavoriteByIds(ids));
    }
}
